<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require_once('vendor/autoload.php');

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\SDK\ViapiUtils\ViapiUtils;

class Aliapi{

    // public function __construct(){
    //     $this->CI= &get_instance();
	// }

    // 微信开发者接入
    public function detect_face($imgurl){
        AlibabaCloud::accessKeyClient(ALI_ACCESSKEY_ID, ALI_ACCESSKEY_SECRET)
        ->regionId('cn-shanghai')
        ->asDefaultClient();
       
        $url = ViapiUtils::upload(ALI_ACCESSKEY_ID, ALI_ACCESSKEY_SECRET, $imgurl);
        // var_export($url);exit;
        try {
            $result = AlibabaCloud::rpc()
                ->product('facebody')
                ->version('2019-12-30')
                ->action('DetectFace')
                ->method('POST')
                ->host('facebody.cn-shanghai.aliyuncs.com')
                ->options([
                            'query' => [
                                'RegionId' => "cn-shanghai",
                                'ImageURL' => $url
                            ],
                        ])
                ->request();
                
                        // echo'<pre>';var_export($result->toArray()['Data']['PoseList']);exit;
                        // return $result->toArray()['Data']['PoseList'];

            if(isset($result->toArray()['Data']['FaceCount'])) {
                return array(
                    'success' => true,
                    'msg' => '',
                    'faceCount' => $result->toArray()['Data']['FaceCount'],
                    'poseList' => $result->toArray()['Data']['PoseList']
                    // 'LandmarkCount' => isset($result->toArray()['Data']['LandmarkCount']) ? $result->toArray()['Data']['LandmarkCount'] : 0
                );
            } else {
                return array(
                    'success' => false,
                    'msg' => '系统错误，请联系管理员',
                );
            }
            // echo'<pre>';print_r($result->toArray()['Data']['FaceCount']);
        } catch (ClientException $exception) {
            $msg = $exception->getErrorMessage();
            return array(
                'success' => false,
                'msg' => $msg
            );
        } catch (ServerException $exception) {
            $msg = $exception->getErrorMessage();
            return array(
                'success' => false,
                'msg' => $msg
            );
        }
    }

}